package com.zz.yt.lib.bluetooth.delegate.print.mht.modle;


import androidx.annotation.NonNull;

import com.mht.print.sdk.PrinterConstants;
import com.mht.print.sdk.PrinterInstance;
import com.mht.print.sdk.Table;


/**
 * 美恒通蓝牙打印
 *
 * @author qf
 * @version 1.0
 **/
public class PrintMhtManager {

    public PrinterInstance mPrinter;

    private static PrintMhtManager mInstance;

    public static PrintMhtManager getInstance(PrinterInstance printer) {
        mInstance = new PrintMhtManager(printer);
        return mInstance;
    }

    private PrintMhtManager(PrinterInstance printer) {
        mPrinter = printer;
    }

    public PrinterInstance getPrinter() {
        return mPrinter;
    }

    /*** 初始化 */
    public PrintMhtManager init() {
        mPrinter.init();
        return mInstance;
    }

    /*** 结尾打印 */
    public void endPrint(int value) {
        // 打印并走纸 n 行
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, value);
    }

    /*** 虚线 */
    public PrintMhtManager drawLine() {
        mPrinter.printText("- - - - - - - - - - - - - - - - - - - - - - -\n");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_NEWLINE);
        return mInstance;
    }

    /**
     * @param title：标题
     */
    public PrintMhtManager setTitle(@NonNull String title) {
        // 横线和纵向都放大字体一倍
        mPrinter.setCharacterMultiple(1, 1);
        //
        mPrinter.setLeftMargin(15, 0);
        // 打印标题
        mPrinter.printText(title + "\n");
        // 打印并换行
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_NEWLINE);
        return mInstance;
    }

    /**
     * @param text：内容单行
     */
    public PrintMhtManager setAline(@NonNull String text) {
        // 恢复默认字体大小
        mPrinter.setCharacterMultiple(0, 0);
        mPrinter.printText(text + "\n");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_NEWLINE);
        return mInstance;
    }

    /**
     * @param paramString：内容单行
     */
    public PrintMhtManager setAline(@NonNull String... paramString) {
        // 恢复默认字体大小
        mPrinter.setCharacterMultiple(0, 0);
        mPrinter.printText(paramString[0] + ": " + paramString[1]
                + "            " +
                paramString[2] + ": " + paramString[3] + "\n");
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_NEWLINE);
        return mInstance;
    }

    /**
     * @param column：内容单行
     * @param columnWidth：值合计最大为48（80cm的打印机）
     */
    public PrintMhtManager setAline(@NonNull String column, int[] columnWidth) {
        // 恢复默认字体大小
        mPrinter.setCharacterMultiple(0, 0);
        return mInstance.setTable(new Table(column, ",", columnWidth));
    }

    /**
     * @param table：表格
     */
    public PrintMhtManager setTable(@NonNull Table table) {
        mPrinter.printTable(table);
        mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_NEWLINE);
        return mInstance;
    }

    /**
     * 信息尾页
     *
     * @param paramString：
     */
    public PrintMhtManager endPrint(@NonNull String... paramString) {
        if (paramString.length > 4) {
            setAline("应付金额 :                    " + paramString[0] + "")
                    .setAline("实付金额 :                    " + paramString[1] + "")
                    .setAline("          ** " + paramString[2] + " **" + "")
                    .setAline("            ** " + paramString[3] + " **" + "")
                    .setAline("          [" + paramString[4] + "]" + "");
            mPrinter.setPrinter(PrinterConstants.Command.PRINT_AND_NEWLINE);
        }
        return mInstance;
    }


    public void printf_80() {
        // 初始化打印机
        init()
                .setTitle("测试收费清单")
                .setAline("单号", "211223001", "业主", "徐秀峰")
                .setAline("日期", "2021-12-23", "时间", "11:12:40")
                .setAline("收费", "测试人员", "房号", "1-1101")
                .drawLine()
                .setTable(PrintUtils.getTable())
                .drawLine()
                .endPrint(" 506.3", " 506.3", "测试物业公司", "测试小区", "监督电话: 023-4008008820")
                .endPrint(3);

    }

}
